/**
 * @(#) MyProfileMediator.as
 *
 * @date		2009. 02. 12.
 * @version		1.0
 * @author		Shin, Yunwoo
 * @description	본인 인사기록 조회 - Mediator
 * 				본인의 인사기록을 조회하는 Presentation Logic을 관리 
 */
package com.skt.common_hr.myprofile_sac.view
{
	import com.skt.common_hr.common.*;
	import com.skt.common_hr.common.components.CommonAlert;
	import com.skt.common_hr.common.puremvc.CommonMediator;
	import com.skt.common_hr.myprofile_sac.MyProfileFacade;
	import com.skt.common_hr.myprofile_sac.model.*;
	import com.skt.common_hr.myprofile_sac.model.vo.CareerVO;
	import com.skt.common_hr.myprofile_sac.view.components.CareerView;
	import com.skt.common_hr.myprofile_sac.view.components.TabSheet3m;
	
	import flash.events.Event;
	
	import mx.collections.ArrayCollection;
	import mx.managers.PopUpManager;
	
	import org.puremvc.as3.multicore.interfaces.INotification;
	

	public class CareerMediator extends CommonMediator
	{
		public static const NAME:String = "CareerMediator"; 

		
		/**
		* 생성자
		* 
		* @param
		* @return void
		*/
        public function CareerMediator( viewComponent:Object )
        {
            super( NAME, viewComponent );
        }
 		





		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Override functions of CommonMediator
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		* View를 초기화 한다.
		*	- View에 기본사항을 출력하고 data를 load
		* @param
		* @return void
		*/
 		override protected function initializeView():void
 		{
			view.addEventListener("ADD_CAREER", onAddCareer);
			view.addEventListener("MOD_CAREER", onModCareer);
			view.addEventListener("DEL_CAREER", onDelCareer);
		}
 		
		/**
		* View에 표현된 데이터들을 삭제한다.
		*	- view가 sleep 으로 들어갈때 실행됨..
		* @param
		* @return void
		*/
 		override protected function clearView():void
 		{
 		}
 		
 		
 		/**
		 * @param
		 * @return void
		 */
		private function loadCareer():void
		{
 			var careerProxy:CareerProxy = facade.retrieveProxy(CareerProxy.NAME) as CareerProxy;
 			careerProxy.load(Variables.myPernr);
 		}
 
 		private function saveCareer():void
 		{
 			var careerProxy:CareerProxy = facade.retrieveProxy(CareerProxy.NAME) as CareerProxy;
 			careerProxy.load(Variables.myPernr);
 		}
 		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Process MVC Messages
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		* 본 Class에서 처리해야할 Noti들을 등록한다.
		* 
		* @param
		* @return void
		*/
		override public function listNotificationInterests():Array 
		{
			return [
					MyProfileFacade.LOAD_PROFILE_DETAIL_COMPLETE,
					CareerProxy.LOAD_CAREER_COMPLETE,
					CareerProxy.LOAD_CAREER_FAILED,
					CareerProxy.MOD_CAREER_COMPLETE,
					CareerProxy.MOD_CAREER_FAILED,
					CareerProxy.INS_CAREER_COMPLETE,
					CareerProxy.INS_CAREER_FAILED,
					CareerProxy.DEL_CAREER_COMPLETE,
					CareerProxy.DEL_CAREER_FAILED
					];
		}
		

		/**
		* Noti별로 처리한다.
		* 
		* @param
		* @return void
		*/
		override public function handleNotification(note:INotification):void
		{
			super.handleNotification(note);
			
			switch(note.getName()) 
			{
				case MyProfileFacade.LOAD_PROFILE_DETAIL_COMPLETE:
					loadCareer();
					break;
				case CareerProxy.LOAD_CAREER_COMPLETE:
					onLoadCareer(note.getBody() as CareerProxy);
					break;
				case CareerProxy.MOD_CAREER_COMPLETE:
					loadCareer();
					closeInputView();
					break;
				case CareerProxy.MOD_CAREER_FAILED:
					CommonAlert.showMessage("", "저장중 오류가 발생했습니다.");
					closeInputView();
					break;
				case CareerProxy.INS_CAREER_COMPLETE:
					loadCareer();
					closeInputView();
					break;
				case CareerProxy.DEL_CAREER_COMPLETE:
					loadCareer();
					break;
				default:
					//trace(this.instanceID+"에서 처리 안된 Noti가 검출됨 : " + note.getBody());
					break;
			}
		}


		private function onLoadCareer(proxy:CareerProxy):void
		{
			view.dataProvider = proxy.careerList;
		}
		







		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	private functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event handler (from View, Internal or....)
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		private var modeModify:Boolean = false;
		private var viewCareer:CareerView = null;
		
		private function onAddCareer(e:Event):void
		{
			modeModify = false;
			if(viewCareer==null) {
				viewCareer  = PopUpManager.createPopUp(view.root, CareerView, true) as CareerView;
				PopUpManager.centerPopUp(viewCareer);
				viewCareer.addEventListener(CareerView.CONFIRM, onInputConfirm);
				viewCareer.addEventListener(CareerView.CANCEL, onInputCancel);
			}
			else {
				PopUpManager.addPopUp(viewCareer, view.root, true);
				PopUpManager.centerPopUp(viewCareer);
			}	
			viewCareer.dataProvider = new CareerVO;
		}
		
		private var deleteList:Array = null;
		private function onDelCareer(e:Event):void
		{
			deleteList = getDeleteList(view.dataProvider);
			if(deleteList.length<1) {
				CommonAlert.showMessage("MyProfile", "삭제할 데이터를 먼저 선택하십시오");
			} 
			else {
				CommonAlert.showMsgYesNo("MyProfile", "삭제하시면 복구 할 수 없습니다. 삭제 하시겠습니까?", null, "확인", view, onConfirmDelete, onCancelDelete, "삭제", "취소");
				//CommonAlert.showMessage("MyProfile", "삭제하시면 복구 할 수 없습니다. 삭제 하시겠습니까?", null, "확인", view.root, onConfirmDelete);
			}
		}
		
		private function getDeleteList(src:ArrayCollection):Array
		{
			var arr:Array = new Array();
			for each(var vo:CareerVO in src) {
				if(vo.selected) {
					arr.push(vo);
				}	
			}
			return arr;
		}
		private function onConfirmDelete():void
		{
			if(deleteList==null) return;
			var proxy:CareerProxy = facade.retrieveProxy(CareerProxy.NAME) as CareerProxy;
			proxy.del(Variables.myPernr, deleteList);
		}	
		
		private function onCancelDelete():void
		{
		}	
		
		private function onModCareer(e:Event):void
		{
			if(view.selectedIndex<0) return;
			
			modeModify = true;
			if(viewCareer==null) {
				viewCareer  = PopUpManager.createPopUp(view.root, CareerView, true) as CareerView;
				PopUpManager.centerPopUp(viewCareer);
				viewCareer.addEventListener(CareerView.CONFIRM, onInputConfirm);
				viewCareer.addEventListener(CareerView.CANCEL, onInputCancel);
	 		}
			else {
				PopUpManager.addPopUp(viewCareer, view.root, true);
				PopUpManager.centerPopUp(viewCareer);
			}
			
			var selectedItem:CareerVO = view.dataProvider.getItemAt(view.selectedIndex) as CareerVO;
			var tmp:CareerVO = new CareerVO();
			tmp.ARBGB = selectedItem.ARBGB;	
			tmp.BEGDA = new Date(selectedItem.BEGDA);	
			tmp.ENDDA = new Date(selectedItem.ENDDA);	
			tmp.SKGYNT = selectedItem.SKGYNT;	
			tmp.LSTJW = selectedItem.LSTJW;	
			tmp.ORT01 = selectedItem.ORT01;	
			tmp.LSTJM = selectedItem.LSTJM;	
			tmp.CONYR = selectedItem.CONYR;	
			tmp.CONMO = selectedItem.CONMO;	
			tmp.SUBTY = selectedItem.SUBTY;	
			tmp.SEQNR = selectedItem.SEQNR;	
			tmp.OBJPS = selectedItem.OBJPS;	
			tmp.SKGYN = selectedItem.SKGYN;	
			viewCareer.dataProvider = tmp;
		}
		
		private function onInputConfirm(e:Event):void
		{
			var proxy:CareerProxy = facade.retrieveProxy(CareerProxy.NAME) as CareerProxy;
			if(modeModify) {
				proxy.modify(Variables.myPernr, viewCareer.dataProvider);
			}
			else {
				proxy.insert(Variables.myPernr, viewCareer.dataProvider);
			}
			
		}

		private function onInputCancel(e:Event):void
		{
			closeInputView();
		}

		private function closeInputView():void
		{
			PopUpManager.removePopUp(viewCareer);
		}

		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Utility Functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
        private function get view():TabSheet3m
        {
            return viewComponent as TabSheet3m;
        }
	}
}